package cluster.traversal;

import text.Text;
import text.TextSetManager;
import text.traversal.ITextTraversal;

public class ComputeTFIDF implements ITextTraversal
{
	public final static String TFIDF = "TFIDF";
	
	private final TextSetManager textSetManager;
	
	private double sum;
	
	public double getSum()
	{
		return this.sum;
	}
	
	public ComputeTFIDF(TextSetManager textSetManager)
	{
		this.textSetManager = textSetManager;
		this.sum = 0;
		
		this.textSetManager.clearWordInfo(TFIDF);
	}
	
	public void traversal(String word, Text text)
	{
		double idf = textSetManager.getInfo(ComputeIDF.IDF, word);
		
		double TFlogId = text.getOccurenceCount(word) / textSetManager.getWordOccurenceSum() * idf;
		
		text.addInfo(TFIDF, word, TFlogId);
	
		this.sum += TFlogId;
	}
}
